package com.phoneSensor.communication;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;



public abstract class ClientBluetoothCommunicate implements BluetoothCommunicateSupper {

	private final Context context;
	private final CommunicateSupporter communSupporter;
	private final List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();

	public ClientBluetoothCommunicate(Context context, CommunicateSupporter supporter) {
		this.context = context;
		this.communSupporter = supporter;
	}

	public void start() {
		deviceList.clear();
		
		// Start service.
		Intent startSrv = new Intent(context, BluetoothClientService.class);
		context.startService(startSrv);
		
		// register broadcast.
		IntentFilter filter = new IntentFilter();
		filter.addAction(BluetoothTools.ACTION_NOT_FOUND_DEVICE);
		filter.addAction(BluetoothTools.ACTION_FOUND_DEVICE);
		filter.addAction(BluetoothTools.ACTION_DATA_RECVED);
		filter.addAction(BluetoothTools.ACTION_CONNECT_SUCCESS);
		
		context.registerReceiver(broadcastReceiver, filter);
	}

	 public void stop() {
		 // Stop service.
		 Intent stopSrv = new Intent(BluetoothTools.ACTION_STOP_SERVICE);
		 context.sendBroadcast(stopSrv);
		 context.unregisterReceiver(broadcastReceiver);
	 }

	 private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(BluetoothTools.ACTION_NOT_FOUND_DEVICE)) {
				
			}
			else if (action.equals(BluetoothTools.ACTION_FOUND_DEVICE)) {
				BluetoothDevice foundedDevice =(BluetoothDevice)
					intent.getExtras().get(BluetoothTools.DEVICE);
				
				if (foundedDevice != null) {
					deviceList.add(foundedDevice);
					foundNewDevice(foundedDevice);
				}
			}
			else if (action.equals(BluetoothTools.ACTION_CONNECT_SUCCESS)) {
				
			}
			else if (action.equals(BluetoothTools.ACTION_DATA_RECVED)) {
				CommunicationBean data = (CommunicationBean)
					intent.getExtras().getSerializable(BluetoothTools.DATA);
				recvData(data.getMsg());
			}
		}
	};
	
	public void connectDevice(BluetoothDevice connDevice) {
		Intent connIntent = new Intent(BluetoothTools.ACTION_CONNECT_TO_DEVICE);
		connIntent.putExtra(BluetoothTools.DEVICE, connDevice);
		context.sendBroadcast(connIntent);
	}
	
	public void startDiscovery() {
		Intent startIntent = new Intent(BluetoothTools.ACTION_START_DISCOVERY);
		context.sendBroadcast(startIntent);
	}
	
	public void sendData(String data) {
		CommunicationBean dataBean = new CommunicationBean();
		dataBean.setMsg(data);

		Intent sendIntent = new Intent(BluetoothTools.ACTION_DATA_TO_SERVICE);
		sendIntent.putExtra(BluetoothTools.DATA, dataBean);
		context.sendBroadcast(sendIntent);
	}

	public Set<BluetoothDevice> getBondedDevices() {
		return BluetoothAdapter.getDefaultAdapter().getBondedDevices();
	}

	public List<BluetoothDevice> getDiscoveriedDevices() {
		return deviceList;
	}

	public void recvData(String data) {
		communSupporter.recvData(data);
		
	}
}
